package com.hdy.orderforthree.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdy.net.order.bean.CategoryBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;

import java.util.List;

import butterknife.BindView;

public class FoodCategoryAdapter  extends BaseAdapter {

    static class ViewHolder extends BaseAdapter.ViewHolder{

        int mIndex;
        @BindView(R2.id.food_category_item) RelativeLayout mView;
        @BindView(R2.id.food_category_name) TextView mName;
        @BindView(R2.id.divider_line) ImageView mDividerLine;
        @BindView(R2.id.food_category_order_number) TextView mCorner;

        public ViewHolder(View view){
            super(view);
        }
    }

    public FoodCategoryAdapter(List list){
        super(R.layout.three_food_category_item, list);
    }

    @Override
    public BaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        BaseAdapter.ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position) {
        CategoryBean foodCategory = (CategoryBean) mList.get(position);
        FoodCategoryAdapter.ViewHolder foodCategoryHolder = (FoodCategoryAdapter.ViewHolder) holder;

        foodCategoryHolder.mIndex = position;
        foodCategoryHolder.mName.setText(foodCategory.getName());
        foodCategoryHolder.mView.setTag(position);
        foodCategoryHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyItemChanged(mSelectedPosition);//刷新之前选择的item
                mSelectedPosition = (int) v.getTag();
                notifyItemChanged(mSelectedPosition);//刷新当前选中点击item
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnItemClick(v, holder, position);
                }
            }
        });
        if (mSelectedPosition == position ) {
            foodCategoryHolder.mView.setBackgroundColor(foodCategoryHolder.mView.getResources().getColor(R.color.foodCategoryItemBackground));
            foodCategoryHolder.mName.setTextColor(foodCategoryHolder.mView.getResources().getColor(R.color.selectedTextColor));
        } else {

            foodCategoryHolder.mView.setBackground(null);
            foodCategoryHolder.mName.setTextColor(foodCategoryHolder.mView.getResources().getColor(R.color.basicTextColor));
        }

        foodCategoryHolder.mCorner.setText(String.valueOf((int)foodCategory.getOrderNumber()));
        if (foodCategory.getOrderNumber() > 0) {
            foodCategoryHolder.mCorner.setVisibility(View.VISIBLE);
        } else {
            foodCategoryHolder.mCorner.setVisibility(View.INVISIBLE);
        }
    }
}
